package org.chengyi.model_shop.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.chengyi.model_shop.entity.Cart;

import java.util.List;

@Mapper
public interface CartMapper {
    @Insert("INSERT INTO cart (user_id, product_id, quantity, created_at, updated_at) VALUES (#{userId}, #{productId}, #{quantity}, #{createdAt}, #{updatedAt})")
    int addCart(Cart cart);
    
    @Select("SELECT * FROM cart WHERE user_id=#{userId}")
    List<Cart> getCartList(Integer userId);
    
    @Select("SELECT * FROM cart WHERE user_id=#{userId} AND product_id=#{productId}")
    Cart getCartByUserIdAndProductId(Integer userId, Integer productId);
    
    @Update("UPDATE cart SET quantity=#{quantity}, updated_at=NOW() WHERE id=#{id}")
    int updateCart(Cart cart);
    
    @Delete("DELETE FROM cart WHERE user_id=#{userId} AND product_id=#{productId}")
    int deleteCart(Integer userId, Integer productId);
    
    @Delete("DELETE FROM cart WHERE user_id=#{userId}")
    int deleteAllCartByUserId(Integer userId);

}
